// pages/user/supplier-express/supplier-express.js
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
let app = getApp()
const STATUS_TEXT = {
  '-1': '已取消',
  0: '待付款',
  1: '待发货',
  2: '已发货',
  25: '部分发货',
  3: '已收货',
  4: '待退款',
  5: '售后中',
  6: '已完成',
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: '',
    STATUS_TEXT,
    tabActive: 0,
    tabsItem: [{
      name: '全部',
      status: '',
    },
      {
        name: '待付款',
        status: 0,
      },
      {
        name: '待配送',
        status: 1,
      },
      {
        name: '已发货',
        status: 2,
      },
      {
        name: '已收货',
        status: 3,
      },
      {
        name: '已完成',
        status: 6,
      },
    ],
    itemsList: [],
    searchValue: '',
    currPage: 1,
    perPage: 7,
    noMore: false,//false 还有更多数据，true 到底了
    isEmpty: false,//true  无数据
    showAlert: false,
    showCompany: false,//控制快递公司弹框
    companyList: [],//快递公司数据
    companySn: '',//快递单号
    companyValue: '',//快递公司
    thenId: '',//确认发货id
    thenOrderId: '',//确认发货ordreId
  },

  //获取快递公司
  getCompanyList () {
    let _this = this
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/expressCodeList',
      data: {

      },
      load: 1
    }).then(res => {
      let temp = [];
      res.data.data.forEach(item =>{
        temp.push(item.msg)
      })
      _this.setData({
        companyList: temp
      })
    })
  },

  //获取列表数据
  getIsKuaidiList (isPage) {//isPage=true下一页，isPage=false第一页
    if (!isPage) this.setData({currPage: 1})
    let _this = this
    let status = _this.data.tabsItem[_this.data.tabActive].status
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/order/isKuaidiList',
      data: {
        tmid: '',
        isKuaidi: 1,
        supplierId: _this.data.user.supplier.id,
        status: status,
        queryItem: _this.data.searchValue || '',
        offset: (_this.data.currPage - 1) * _this.data.perPage,
        limit: _this.data.perPage
      },
      load: 1
    }).then(res => {
      let tempList = res.data.data.rows
      if (isPage) {//下一页的数据拼接在原有数据后面
        _this.setData({
          itemsList: _this.data.itemsList.concat(tempList)
        })
      }
      else {//第一页数据直接赋值
        _this.setData({
          itemsList: tempList,
          isEmpty: !tempList.length > 0,
          noMore: false,
        })
      }
      //如果返回的数据为空，那么就没有下一页了
      if (_this.data.totalPage == res.data.data.total) {
        _this.setData({
          noMore: true,
        })
      }
    })
  },

  //弹框关闭，阻止关闭
  onClose (event) {
    let _this = this
    if (event.detail == 'confirm') {
      if(_this.data.companyValue == ''){
        wx.showToast({icon:'none',title: '请选择快递公司'})
        _this.setData({showAlert: true})
        return;
      } else if(_this.data.companySn == ''){
        wx.showToast({icon:'none',title: '请填写快递单号'})
        _this.setData({showAlert: true})
        return;
      }
      app.requestUrl({
        method: 'POST',
        url: '/wxapi/order/sendIsKuaidiGoods',
        data: {
          kuaidisn: _this.data.companySn,
          expressCode: _this.data.companyValue,
          id: _this.data.thenId,
          orderid: _this.data.thenOrderId
        }
      }).then(res =>{
        _this.data.itemsList.forEach((item,index) => {
          if(item.id == _this.data.thenId){
            _this.setData({
              ['itemsList[' + index +'].status']: 2,
            })
          }
        })
        _this.setData({
          thenId: '',
          thenOrderId: '',
        })
      })
    }
    else if (event.detail == 'cancel') {

    }
  },

  showShow(event){
    this.setData({
      thenId: event.currentTarget.dataset.id,
      thenOrderId: event.currentTarget.dataset.orderid,
      showAlert: true
    })
  },
  //搜索
  onSearch (event) {
    this.setData({
      searchValue: event.detail
    })
    this.getIsKuaidiList(false)
  },
  //搜索点击
  onSearchClick () {
    this.getIsKuaidiList(false)
  },
  //搜索值改变
  onChange (event) {
    this.setData({
      searchValue: event.detail
    })
  },

  toCompanyConfirm(event){
    let _this = this;
    _this.setData({
      companyValue: event.detail.value
    })
    _this.toCompanyMonitor();
  },
  toCompanyMonitor(){
    let _this = this;
    _this.setData({
      showCompany: !_this.data.showCompany
    })
  },
  onCompanyBlur(event){
    this.setData({
      companySn: event.detail.value
    })
  },

  toOrderInfo(event){
    wx.navigateTo({
      url: '/pages/user/order-info/order-info?payId=' + event.currentTarget.dataset.orderid +
          '&id=' + event.currentTarget.dataset.id + '&iskuaidi=1',
    })
  },

  tabChange (event) {
    let _this = this
    _this.setData({
      tabActive: event.detail.index
    })
    _this.getIsKuaidiList()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      user: app.getUser(),
    })
    this.getIsKuaidiList(false)
    this.getCompanyList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.noMore) {
      this.setData({
        currPage: this.data.currPage + 1
      })
      this.getIsKuaidiList(true)//传true代表进行下一页
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})